# coding: utf-8
from typing import Literal

from common import cfg, LENOVO_USER_AGENT
from .send import RequestBase


class LenovoAPI(RequestBase):
    """
    联想壁纸请求数据
    """
    base_url = 'https://pc-store.lenovomm.cn'
    wallpaper_url = base_url + '/wallpapercontent/wallpaper'
    headers = {
        'user-agent': LENOVO_USER_AGENT,
        'token': cfg.get(cfg.lenovoToken),
        'h5need': '1111111111111111111',
        'h5version': '9.0.2230.0617'
    }

    @classmethod
    def get_token(cls):
        json = {
            "channelInfo": {
                "businessChannel": "Nil"
            },
            "commonData": {
                "channel": "20005",
                "from": "indstore",
                "mac": "7413ea7e8dbc",
                "mt": "10.0.22621",
                "os": "6.1.50.12061",
                "osBit": "64",
                "pcmStoreVer": "9.0.1200.1220",
                "pcmVer": "6.1.50.12061",
                "preVersion": "",
                "sn": "123108Z0M768120852C",
                "uuid": "7D7A7E5C-5ACE-11EE-8DC7-D47E00373F00",
                "ver": "9.0.1330.201",
                "versionCode": "9.0.1330.201",
                "versionName": "9.0.1330.201"
            },
            "settingInfo": {
                "smbOptions": "1"
            }
        }
        url = cls.base_url + '/sysconfigservice/indep/business_parameter'
        response = cls.post(url, json=json, headers=cls.headers)
        token = response.json().get('data', {}).get('btoken', '')
        cfg.set(cfg.lenovoToken, token)
        return token

    @classmethod
    def banner(cls, page_type: Literal['wallpaper', 'wallpaperHot', 'wallpaperRank'] = 'wallpaper'):
        """
        精选、热门、排行
        顶部横幅内容
        :param page_type:
        :return:
        """
        url = cls.wallpaper_url + '/banner/top_banner_content_v2'
        params = {'pageType': page_type}
        response = cls.get(url, params=params, headers=cls.headers)
        return response.json()

    @classmethod
    def rank_more(cls, rank_id: int = 2707, *, limit: int = 48, skip: int = 0):
        """
        排行
        :param rank_id:
        :param limit:
        :param skip:
        :return
        """
        url = cls.wallpaper_url + '/rank_more'
        params = {
            'code': rank_id,
            'limit': limit,
            'skip': skip,
            'wallpaperId': '',
            'bizInfo': ''
        }
        return cls.get(url, params=params, headers=cls.headers).json()

    @classmethod
    def classes(cls):
        """
        壁纸分类接口
        :return:
        """
        url = cls.wallpaper_url + '/classes'
        return cls.get(url, headers=cls.headers).json()

    @classmethod
    def group_content(cls, page_type: Literal['wallpaper', 'wallpaperHot', 'wallpaperRank'] = 'wallpaper'):
        """
        页面内容
        :param page_type:
        :return:
        """
        url = cls.wallpaper_url + '/group_content'
        params = {'pageType': page_type}
        return cls.get(url, params=params, headers=cls.headers).json()

    @classmethod
    def class_wallpaper_list(cls, classId: int = -1, tagId: int = -1, priceId: int = 0, skip: int = 0):
        """
        根据分类接口获取壁纸信息
        :return:
        """
        url = cls.wallpaper_url + "/class_wallpaper_list"
        params = {
            "skip": skip,
            "limit": 40,
            "tagId": tagId,
            "colorId": -1,
            "classId": classId,
            "refresh": False,
            "priceId": priceId
        }
        return cls.get(url, params=params, headers=cls.headers).json()
